package com.scalar.chiptrack.operations.dataentry.transit.actioncommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.transit.forms.TransitDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.LotDetails;

import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Sep 24, 2006
 * Time: 12:13:56 AM
 */
public class TransitDataEntryLoadPartNumbersCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/transitDataEntry.do" ;

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        TransitDataEntryForm transitDataEntryForm = ( TransitDataEntryForm ) form;

        HashMap<String,String> errors = new HashMap<String,String>();
        HashMap<String,String> info   = new HashMap<String,String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        ArrayList<LabelValueBean> transitPartNumbersLableValueBeans = new ArrayList<LabelValueBean>();

       try
       {
           transitDataEntryForm.setProcessStepId("0");
           transitDataEntryForm.setLocationId("0");
           transitDataEntryForm.setShippedLotId("0");
           transitDataEntryForm.setFabLotNo("0");

           transitDataEntryForm.setBaseprocessType( NewProductDAO.getBaseProcessName( null, transitDataEntryForm.getProcessType() ));

           if ( transitDataEntryForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_FAB ) || transitDataEntryForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || transitDataEntryForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_SORT ) || "Back Grind".equalsIgnoreCase(transitDataEntryForm.getProcessType() )
                    || transitDataEntryForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_DIE_BANK )  )
            {
                 request.setAttribute("showWaferIds", "TRUE");
            }
            else
            {
                request.setAttribute("showWaferIds", "FALSE");
            }
           ArrayList<PartNumber>  partNumberList = DataEntryDAO.getPartNumberByPsType( transitDataEntryForm.getProcessType() );

           for ( int i = 0; i < partNumberList.size (); i++ )
           {
               PartNumber partNumber = ( PartNumber ) partNumberList.get ( i );
               transitPartNumbersLableValueBeans.add ( new org.apache.struts.util.LabelValueBean ( partNumber.getPartNumber()/*+"/"+ partNumber.getProcessType()*/, partNumber.getProcessStepId() ) );
           }
           request.setAttribute("processTypeList", NewProductDAO.loadAllProcessesFromDB( null, false));
           request.setAttribute( "partNumbersList", transitPartNumbersLableValueBeans );
           request.setAttribute( "action", action );
       }
       catch( Exception e)
       {
          e.printStackTrace();
       }
       return mapping.findForward( "transitDataEntry" );
    }
}
